package cn.gok.oa.system.service.impl;

import cn.gok.oa.system.dto.PermissionDto;
import cn.gok.oa.system.dto.UserListDto;
import cn.gok.oa.system.entity.TbMenuRole;
import cn.gok.oa.system.entity.TbUser;
import cn.gok.oa.system.mapper.TbMenuRoleMapper;
import cn.gok.oa.system.mapper.TbUserMapper;
import cn.gok.oa.system.service.ITbMenuRoleService;
import cn.gok.oa.system.vo.UserPermissionVo;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gok
 * @since 2022-08-04
 */
@Service
public class TbMenuRoleServiceImpl extends ServiceImpl<TbMenuRoleMapper, TbMenuRole> implements ITbMenuRoleService {

    @Autowired
    TbUserMapper tbUserMapper;

    @Autowired
    TbMenuRoleMapper tbMenuRoleMapper;

//    @Override
//    public IPage<TbUser> getUserRoleList(UserListDto userListDto) {
//        Page<TbUser> page = new Page<>(userListDto.getPage(), userListDto.getLimit());
//        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("role_id",userListDto.getRoleId());
//        return tbUserMapper.selectPage(page,queryWrapper);
//    }
//
//    @Override
//    public IPage<TbUser> getUserIsNotRoleList(UserListDto userListDto) {
//        Page<TbUser> page = new Page<>(userListDto.getPage(), userListDto.getLimit());
//        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
//        queryWrapper.ne("role_id",userListDto.getRoleId());
//        return tbUserMapper.selectPage(page,queryWrapper);
//    }

    @Override
    public void allotUserPermission(PermissionDto permissionDto, HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        Integer modifyId = Integer.parseInt(JWT.decode(token).getAudience().get(0));
        // 遍历用户id字段并更新用户表中当前id下的role_id字段
        int update = 0;
        for (Integer userId: permissionDto.getUserIds()){
            UpdateWrapper<TbUser> userUpdateWrapper = new UpdateWrapper<>();
            userUpdateWrapper
                    .set("role_id", permissionDto.getRoleId())
                    .set("modify_id", modifyId)
                    .eq("id", userId);
            update = tbUserMapper.update(null, userUpdateWrapper);
        }

        if (update>0){
            return;
        } throw new RuntimeException("分配角色异常");
    }


    @Override
    public void cancelUserPermission(Integer userId, HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        Integer modifyId = Integer.parseInt(JWT.decode(token).getAudience().get(0));
        // 更新用户表 将role_id置为4(none用户没有权限)
        UpdateWrapper<TbUser> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper
                .set("role_id", 4)
                .set("modify_id", modifyId)
                .eq("id", userId);
        int update = tbUserMapper.update(null, userUpdateWrapper);
        if (update>0){
            return;
        } throw new RuntimeException("取消授权异常");
    }

    @Override
    public void cancelAllUserPermission(PermissionDto permissionDto, HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        Integer modifyId = Integer.parseInt(JWT.decode(token).getAudience().get(0));
        // 遍历用户id字段并将用户表中当前id下的role_id置为4(none用户没有权限)
        int update = 0;
        for (Integer userId: permissionDto.getUserIds()){
            UpdateWrapper<TbUser> userUpdateWrapper = new UpdateWrapper<>();
            userUpdateWrapper
                    .set("role_id", 4)
                    .set("modify_id", modifyId)
                    .eq("id", userId);
            update = tbUserMapper.update(null, userUpdateWrapper);
        }
        if (update>0){
            return;
        } throw new RuntimeException("取消授权异常");
    }

    @Override
    public IPage<UserPermissionVo> getList(Page<UserPermissionVo> page, Integer roleId) {
        return tbMenuRoleMapper.getList(page, roleId);
    }


    @Override
    public IPage<UserPermissionVo> getListNone(Page<UserPermissionVo> page, Integer roleId) {
        return tbMenuRoleMapper.getListNone(page, roleId);
    }
}
